import pandas as pd
from time import time
import matplotlib.pyplot as plt

def read_data(location):
	# location = 'C:\sandbox\ML\data\XAUUSD.csv'
	return pd.read_csv(location)


def read_data_and_index_by_date(location):
	df = pd.read_csv(location, index_col = "Date", parse_dates=True, usecols=['Date', 'Close'])
	return df

def fillter_data_by_date(location, start_date, end_date):
	df = read_data_and_index_by_date(location);
	return df.ix[start_date:end_date]

def how_long(func, *args):
	t0 = time()
	result = func(*args)
	t1 = time()
	return result, (t1 - t0)

def plot_data(df, title, x_label, y_label):
	ax = df.plot(title=title, fontsize=12)
	ax.set_xlabel(x_label)
	ax.set_ylabel(y_label)
	plt.show()

def get_rolling_mean(values, window):
    return values.rolling(window=20,center=False).mean()

def compute_daily_returns(df):
	daily_returns = (df / df.shift(1)) - 1
	daily_returns.ix[0, :] = 0
	return daily_returns


# def testing():
# 	df = pd.DataFrame({'AAA' : [1,2,1,3], 'BBB' : [1,1,2,2], 'CCC' : [2,1,3,1]});
# 	print compute_daily_returns(df)
	


if __name__ == "__main__":
	location = r'C:\sandbox\ML\data\10_Y_DXAUUSD.csv'
	df_xau = read_data_and_index_by_date(location)
	df_xau_daily_return = compute_daily_returns(df_xau)
	# print df_xau_daily_return
	# df_xau = fillter_data_by_date(location, '20170102','20171030')
	# df_xau_rm = get_rolling_mean(df_xau['Close'], window=20)
	plot_data(df_xau_daily_return,  "Ten Years XAU Daily Return", "Date", "Daily_Return")
	testing()